package fluxx.draft;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class CardPile {
	Queue<Card> cards;
	public CardPile () {
		cards = new LinkedList<Card>(); // TODO Choose impl.
	}
	public CardPile (Set<Card> cardsToAdd) {
		this();
		for (Card card : cardsToAdd) {
			add(card);
		}
		shuffle();
	}
	public void add(Card card) {
		cards.add(card);
	}
	public Card take() {
		if (cards.size() == 0) {
			// ???
		}
		return cards.remove();
	}
	public Card[] take(int amount) {
		Card[] returnCards = new Card[amount];
		for (int i = 0; i < amount; i++) {
			returnCards[i] = take();
		}
		return returnCards;
	}
	public void shuffle() {
		// We need to use remove(int) from LinkedList
		LinkedList<Card> oldCards = (LinkedList<Card>) cards;
		Queue<Card> newCards = new LinkedList<Card>();
		int random;
		// Copy a random element from old pile to new pile.
		while (!oldCards.isEmpty()) {
			random = (int) (Math.random() * oldCards.size());
			newCards.add(oldCards.remove(random));
		}
		cards = newCards;
	}
	public int size() {
		return cards.size();
	}
}
